package com.example.administrator.newsapp.DialogManagers;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.administrator.newsapp.AdapterManagers.Decoration;
import com.example.administrator.newsapp.AdapterManagers.MyAdapter;
import com.example.administrator.newsapp.Datas.Channel;
import com.example.administrator.newsapp.R;

import java.util.ArrayList;
import java.util.List;

public class EditDialog {
    private Context context;
    private Dialog dialog;
    private RecyclerView recyclerView;
    private GridLayoutManager layoutManager;
    private MyAdapter myAdapter;
    private static List<Channel> list;
    static {
        list=new ArrayList<>();
        for(int i=0;i<1;i++){
            Channel channel=new Channel();
            channel.type=1;
            channel.channel_title="我的频道";
            channel.channel_words="点击进入频道";
            list.add(channel);
        }
        for(int i=0;i<5;i++){
            Channel channel=new Channel();
            channel.type=3;
            channel.channer_name="频道"+i;
            list.add(channel);
        }
        for(int i=0;i<1;i++){
            Channel channel=new Channel();
            channel.type=2;
            channel.channel_title="推荐频道";
            channel.channel_words="点击添加频道";
            list.add(channel);
        }
        for(int i=0;i<5;i++){
            Channel channel=new Channel();
            channel.type=4;
            channel.channer_name="频道"+i;
            list.add(channel);
        }

    }
    public EditDialog(Context context) {
        this.context = context;
    }

    public void showEditDialog(){
        dialog=new Dialog(context, R.style.editdialog);
        LayoutInflater inflater=LayoutInflater.from(context);
        View view=inflater.inflate(R.layout.editdialog,null);

        dialog.setContentView(view);
        recyclerView=view.findViewById(R.id.editdialog_recycle);
        layoutManager=new GridLayoutManager(view.getContext(),4);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int i) {
                int type=list.get(i).type;
                    if (type==1){
                        return 4;
                    }else if (type==2){
                        return 4;
                    }else if (type==3){
                        return 1;
                    }else if (type==4){
                        return 1;
                    }
                    return 0;

            }
        });
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new Decoration(10));
        myAdapter=new MyAdapter(list,view.getContext());
        recyclerView.setAdapter(myAdapter);
        Window window=dialog.getWindow();
        window.setWindowAnimations(R.style.editdialogAnimation);
        WindowManager.LayoutParams layoutParams=window.getAttributes();

        int height=view.getResources().getIdentifier("status_bar_height","dimen","android");
//        //转化为像素值
        int res=view.getResources().getDimensionPixelSize(height);
//        layoutParams.y=res;
        layoutParams.width=ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height=ViewGroup.LayoutParams.MATCH_PARENT-res;
        window.setAttributes(layoutParams);

        dialog.setCanceledOnTouchOutside(true);
        dialog.show();

    }
}
